package com.daiqee.util;

import java.awt.Color;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 验证码生成工具
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V1.0
 * @author RichardTang 
 * @date: 2018年2月1日 下午6:00:27
 * @package com.daiqee.util
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
public class ValidateCodeUtils {
	private int width = 90;
	private int height = 40;
	private int codeCount = 4;
	private int lineCount = 19;
	private int fontSize = 20;
	private String sessionKey = "captcha";

	public ValidateCodeUtils() {
	}

	public ValidateCodeUtils(int width, int height, int fontSize) {
		this.width = width;
		this.height = height;
		this.fontSize = fontSize;
	}

	public ValidateCodeUtils(int width, int height, int fontSize, String sessionKey) {
		this.width = width;
		this.height = height;
		this.fontSize = fontSize;
		this.sessionKey = sessionKey;
	}

	public ValidateCodeUtils(int width, int height, int codeCount, int fontSize, String sessionKey) {
		this.width = width;
		this.height = height;
		this.codeCount = codeCount;
		this.fontSize = fontSize;
		this.sessionKey = sessionKey;
	}

	public ValidateCodeUtils(int width, int height, int codeCount, int lineCount, int fontSize, String sessionKey) {
		this.width = width;
		this.height = height;
		this.codeCount = codeCount;
		this.lineCount = lineCount;
		this.fontSize = fontSize;
		this.sessionKey = sessionKey;
	}

	char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R',
			'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	public void getCode(HttpServletRequest request, HttpServletResponse response) {
		Random r = new Random();

		StringBuilder builderCode = new StringBuilder();

		BufferedImage buffImg = new BufferedImage(this.width, this.height, 1);

		Graphics g = buffImg.getGraphics();

		g.setColor(Color.gray);
		g.drawRect(0, 0, this.width, this.height);

		g.setColor(Color.white);
		g.fillRect(1, 1, this.width - 2, this.height - 2);
		for (int i = 0; i < this.lineCount; i++) {
			int _R = (int) Math.floor(Math.random() * 256.0D);
			int _G = (int) Math.floor(Math.random() * 256.0D);
			int _B = (int) Math.floor(Math.random() * 256.0D);
			g.setColor(new Color(_R, _G, _B, 255));
			g.drawLine(r.nextInt(this.width), r.nextInt(this.width), r.nextInt(this.width), r.nextInt(this.width));
		}
		g.setColor(Color.blue);

		g.setFont(new Font("宋体", 3, this.fontSize));
		for (int i = 0; i < this.codeCount; i++) {
			String c = String.valueOf( this.codeSequence[r.nextInt(this.codeSequence.length)]);
			builderCode.append(c);
			g.drawString(c, this.width / this.codeCount * i + 2, this.height * 4 / 5);
		}
		try {
			ServletOutputStream sos = response.getOutputStream();
			ImageIO.write(buffImg, "png", sos);

			HttpSession session = request.getSession();
			session.setAttribute(this.sessionKey, builderCode.toString());

			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0L);
			response.setContentType("image/png");

			sos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
